package edu.cuit.avatar.filter;


import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/1 16:02
 */
@WebFilter("/book/*")
public class CorsFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain chain) throws IOException, ServletException {
        var response = (HttpServletResponse)servletResponse;
        response.setHeader("Access-Control-Allow-Origin","*"); //设置允许跨域的url请求
        response.setHeader("Access-Control-Allow-Methods","POST,OPTIONS,GET,PUT,DELETE"); //设置允许跨域的HTTP请求方法
        response.setHeader("Access-Control-Max-Age", "3600"); //max-age
        response.setHeader("Access-Control-Allow-Headers","Content-Type, Authorization");
        //跨域请求允许携带cookie凭证
        //response.setHeader("Access-Control-Allow-Credentials","true");
        chain.doFilter(servletRequest, response);
    }
}
